package com.unittec.zk.provider.flowable.service.impl;

import com.unittec.zk.provider.flowable.common.constant.ServiceIdConstants;
import com.unittec.zk.provider.flowable.service.ISysFormService;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.scope.ThreadLocalScope;
import com.unittec.zk.provider.service.HystrixRestTemplateService;
import com.unittec.zk.sdk.auth.domain.SysFormDto;
import com.unittec.zk.sdk.auth.request.*;
import com.unittec.zk.sdk.auth.response.*;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;
import com.unittec.zk.sdk.root.internal.exception.BusinessException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class SysFormServiceImpl implements ISysFormService
{
    @Autowired
    Log log;

    @Resource
    private HystrixRestTemplateService hystrixRestTemplateService;

    /**
     * 查询流程表单
     *
     * @param formId 流程表单ID
     * @return 流程表单
     */
    @Override
    public SysFormDto selectSysFormById(Long formId)
    {
        log.sysInfoLog("selectSysFormById ", "begin",formId);
        try {
            GatewayRequest<Req101673> getWay = new GatewayRequest<>();
            Req101673 req = new Req101673();
            req.setId(formId);
            getWay.setContent(req);
            getWay.setMethod("101673");
            Result<Res101673> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101673, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101673>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "查询流程表单失败");
                throw new BusinessException("查询流程表单失败");
            }else{
                if (result.getData() != null ) {//&& result.getData().geContent() != null
                    SysFormDto sysForm = result.getData().getSysForm();//geContent()
                    return sysForm;
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("selectSysFormById", "查询流程表单失败", e);
            throw new BusinessException("查询流程表单失败");
        }
        log.sysInfoLog("selectSysFormById", "end");
        return null;
    }

    /**
     * 查询流程表单列表
     *
     * @param sysForm 流程表单
     * @return 流程表单
     */
    @Override
    public List<SysFormDto> selectSysFormList(SysFormDto sysForm)
    {
        log.sysInfoLog("selectSysFormList ", "begin",sysForm);
        try {
            GatewayRequest<Req101671> getWay = new GatewayRequest<>();
            Req101671 req = new Req101671();
            req.setSysForm(sysForm);
            getWay.setContent(req);
            getWay.setMethod("101671");
            Result<Res101671> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101671, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101671>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "查询流程表单列表失败");
                throw new BusinessException("查询流程表单列表失败");
            }else{
                if (result.getData() != null ) {//&& result.getData().geContent() != null
                    List<SysFormDto> list = result.getData().getList();
                    return  list;
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("selectSysFormList", "查询流程表单列表失败", e);
            throw new BusinessException("查询流程表单列表失败");
        }
        log.sysInfoLog("selectSysFormList", "end");
        return  null;
    }

    /**
     * 新增流程表单
     *
     * @param sysForm 流程表单
     * @return 结果
     */
    @Override
    public int insertSysForm(SysFormDto sysForm)
    {
        log.sysInfoLog("insertSysForm ", "begin",sysForm);
        try {
            GatewayRequest<Req101674> getWay = new GatewayRequest<>();
            Req101674 req = new Req101674();
            req.setSysForm(sysForm);
            getWay.setContent(req);
            getWay.setMethod("101674");
            Result<Res101674> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101674, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101674>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "新增流程表单失败");
                throw new BusinessException("新增流程表单失败");
            }else{
                if (result.getData() != null ) {//&& result.getData().geContent() != null
                    String content = result.getData().toString();//toString()
                    if(content == "表单导入成功"){
                        return 0;
                    }else{
                        return  1;
                    }
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("insertSysForm", "新增流程表单失败", e);
            throw new BusinessException("新增流程表单失败");
        }
        log.sysInfoLog("insertSysForm", "end");
        return  1;
    }

    /**
     * 修改流程表单
     *
     * @param sysForm 流程表单
     * @return 结果
     */
    @Override
    public int updateSysForm(SysFormDto sysForm)
    {
        log.sysInfoLog("updateSysForm ", "begin",sysForm);
        try {
            GatewayRequest<Req101675> getWay = new GatewayRequest<>();
            Req101675 req = new Req101675();
            req.setSysForm(sysForm);
            getWay.setContent(req);
            getWay.setMethod("101675");
            Result<Res101675> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101675, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101675>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "修改流程表单失败");
                throw new BusinessException("修改流程表单失败");
            }else{
                if (result.getData() != null ) {//&& result.getData().geContent() != null
                    String content = result.getData().toString();//toString()
                    if(content == "表单修改成功"){
                        return 0;
                    }else{
                        return  1;
                    }
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("updateSysForm", "修改流程表单失败", e);
            throw new BusinessException("修改流程表单失败");
        }
        log.sysInfoLog("updateSysForm", "end");
        return  1;
    }

    /**
     * 批量删除流程表单
     *
     * @param formIds 需要删除的流程表单ID
     * @return 结果
     */
    @Override
    public int deleteSysFormByIds(Long[] formIds)
    {
        log.sysInfoLog("deleteSysFormByIds ", "begin",new Object());
        try {
            GatewayRequest<Req101676> getWay = new GatewayRequest<>();
            Req101676 req = new Req101676();
            req.setIds(formIds);
            getWay.setContent(req);
            getWay.setMethod("101676");
            Result<Res101676> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101676, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101676>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "批量删除流程表单失败");
                throw new BusinessException("批量删除流程表单失败");
            }else{
                if (result.getData() != null ) {//&& result.getData().geContent() != null
                    String content = result.getData().toString();//toString()
                    if(content == "表单删除成功"){
                        return 0;
                    }else{
                        return  1;
                    }
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("deleteSysFormByIds", "批量删除流程表单失败", e);
            throw new BusinessException("批量删除流程表单失败");
        }
        log.sysInfoLog("deleteSysFormByIds", "end");
        return  1;
    }

    /**
     * 删除流程表单信息
     *
     * @param formId 流程表单ID
     * @return 结果
     */
    @Override
    public int deleteSysFormById(Long formId)
    {
        log.sysInfoLog("deleteSysFormById ", "begin");
        try {
            GatewayRequest<Req101676> getWay = new GatewayRequest<>();
            Req101676 req = new Req101676();
            Long[] ids = new Long[1];
            ids[0] = formId;
            req.setIds(ids);
            getWay.setContent(req);
            getWay.setMethod("101673");
            Result<Res101673> result = hystrixRestTemplateService.exchange(ServiceIdConstants.AUTH_101673, getWay,
                    ThreadLocalScope.getAuthData(), new ParameterizedTypeReference<Result<Res101673>>() {});
            if (!result.isSuccess()) {
                log.error(getWay, BizCode.SAVE_ERROR.getCode(), "删除流程表单信息失败");
                throw new BusinessException("删除流程表单信息失败");
            }else{
                if (result.getData() != null ) {//&& result.getData().geContent() != null
                    String content = result.getData().toString();//toString()
                    if(content == "表单删除成功"){
                        return 0;
                    }else{
                        return  1;
                    }
                }
            }
        } catch (BeansException e) {
            log.sysErrLog("deleteSysFormById", "删除流程表单信息失败", e);
            throw new BusinessException("删除流程表单信息失败");
        }
        log.sysInfoLog("deleteSysFormById", "end");
        return  1;
    }
}
